$(document).ready(function(){
	/**
	 * flag:为true时表示输入正确，为false时表示提示信息
	 */
	var prompt = function(text, message, flag) {
		if(flag) {
			$('#'+text.id+'Prompt').addClass("qb_none");
			$('#'+text.id+'Prompt').html('');
		}else {
			if(message != '')
			{
				$('#'+text.id+'Prompt').removeClass("qb_none");
				$('#'+text.id+'Prompt').html('<span>'+message+'</span>');
			}
		}
	};
	
	var promptFailure = function(text, message) {
		if(message != '')
		{
			$('#'+text.id+'Prompt').removeClass("qb_none");
			$('#'+text.id+'Prompt').html('<span>'+message+'</span>');
		}
	};
	
	/**
	 * 失去焦点时密码检查
	 */
	var checkPwdBlur = function(name) {
		var value = $("#I"+name).val();
		var msg = Helper.validata.validataPass(value);
		if(undefined != msg && '' != msg){
			promptFailure($("#I"+name)[0], msg);
			return;
		}
		prompt($("#I"+name)[0], "", true);
		return true;
	};
	
	/**
	 * 获得焦点时密码检查
	 */
	var checkPwdFocus = function(name, msg){
		prompt($("#I"+name)[0], msg, false);
	};
	
	/**
	 * 新密码是否和旧密码相同
	 */
	var notEqNewAndOldPwd = function(){
		var IoldPwd = $("#IoldPwd").val();
		var InewPwd = $("#InewPwd").val();
		if(IoldPwd === InewPwd) {
			promptFailure($("#InewPwd")[0], "旧密码和新密码不能相同");
			return;
		}
		return true;
	};
	
	/**
	 * 检查重复密码
	 */
	var checkReNewPwd = function(){
		var InewPwd = $("#InewPwd").val();
		var IreNewPwd = $("#IreNewPwd").val();
		if(IreNewPwd != InewPwd) {
			promptFailure($("#IreNewPwd")[0], "重复密码和新密码不一致");
			return;
		}
		return true;
	};
	
	/**
	 * 密码框初始化
	 */
	var commInit = function(type) {
		/**
		 * 旧密码
		 */
		$("#IoldPwd").blur(function(){
			checkPwdBlur("oldPwd");
		});
		
		$("#IoldPwd").focus(function(){
			checkPwdFocus("oldPwd", "");
		});

		/**
		 * 新密码
		 */
		$("#InewPwd").blur(function(){
			if(checkPwdBlur("newPwd"))
			{
				notEqNewAndOldPwd();
			}
		});
		
		$("#InewPwd").focus(function(){
			checkPwdFocus("newPwd", "");
		});
		
		/**
		 * 重复密码
		 */
		$("#IreNewPwd").blur(function(){
			if(checkPwdBlur("reNewPwd"))
			{
				checkReNewPwd();
			}
		});
		
		/*if(type == 2 && $("#IisSet").val() == 0)
		{
			$("#InewPwd").focus();
		}
		else
		{
			$("#IoldPwd").focus();
		}*/
	};
	
	
	/**
	 * 支付密码初始化
	 */
	var payPwdInit = function() {
		var isSetPwd = $("#IisSet").val();
		/**
		 * 修改支付密码
		 */
		var updtePayPwd = new Helper.RemoteTrans( {
			url: Helper.basePath + "/member_operate/doUpdatePayPwd.htm",
			onSuccess: function(result){
				Helper.confirm("恭喜您，"+(isSetPwd===1?"修改":"设置")+"成功！");
				if($("#3338login").val()==3338){
					location.href = Helper.basePath+'/page/3338web/memberInfo/memberIndex.jsp';
				}else{
					location.href = Helper.basePath+'/page/memberinfo/';
				}
				
			},
			onError: function(result, status){
				Helper.message((result.message==null?(isSetPwd===1?"修改":"设置")+"失败":result.message));
			}
		});
		
		/**
		 * 确认修改
		 */
		var IisSet = $("#IisSet").val();
		$("#payPwdUpdate").click(function(){
			if(IisSet == 1) {
				if(checkPwdBlur("oldPwd") && checkPwdBlur("newPwd") 
					&& notEqNewAndOldPwd() && checkReNewPwd()){
					updtePayPwd.addParameters("oldPwd", $("#IoldPwd").val());
					updtePayPwd.addParameters("newPwd", $("#InewPwd").val());
					updtePayPwd.send();
				}
			}else {
				if(checkPwdBlur("newPwd") && checkReNewPwd()){
					updtePayPwd.addParameters("oldPwd", $("#IoldPwd").val());
					updtePayPwd.addParameters("newPwd", $("#InewPwd").val());
					updtePayPwd.send();
				}
			}
		});
		
		$("#html5payPwdUpdate").click(function(){
			if(IisSet == 1) {
				if(checkPwd("oldPwd") && checkPwd("newPwd") 
					&& eqNewAndOldPwd() && checkReNewPwdAlert()){
					updtePayPwd.addParameters("oldPwd", $("#IoldPwd").val());
					updtePayPwd.addParameters("newPwd", $("#InewPwd").val());
					updtePayPwd.send();
				}
			}else {
				if(checkPwd("newPwd") && checkReNewPwdAlert()){
					updtePayPwd.addParameters("oldPwd", $("#IoldPwd").val());
					updtePayPwd.addParameters("newPwd", $("#InewPwd").val());
					updtePayPwd.send();
				}
			}
		});
		
		/**
		 * 失去焦点时密码检查
		 */
		var checkPwd = function(name) {
			var value = $("#I"+name).val();
			var msg = Helper.validata.validataPass(value);
			if(undefined != msg && '' != msg){
				alert(msg);
				return;
			}
			prompt($("#I"+name)[0], "", true);
			return true;
		};
		
		var eqNewAndOldPwd = function(){
			var IoldPwd = $("#IoldPwd").val();
			var InewPwd = $("#InewPwd").val();
			if(IoldPwd === InewPwd) {
				alert("旧密码和新密码不能相同");
				return;
			}
			return true;
		};
		
		/**
		 * 检查重复密码
		 */
		var checkReNewPwdAlert = function(){
			var InewPwd = $("#InewPwd").val();
			var IreNewPwd = $("#IreNewPwd").val();
			if(IreNewPwd != InewPwd) {
				alert("重复密码和新密码不一致");
				return;
			}
			return true;
		};
		
		$("#pwdupdate").click(function(){
			/**
			 * 修改支付密码
			 */
			var updteLogPwd = new Helper.RemoteTrans( {
				url: Helper.basePath + "/member_operate/doUpdateLoginPwd.htm",
				onSuccess: function(result){
					Helper.message("恭喜您修改成功！");
					if($("#3338login").val()==3338){
						location.href = Helper.basePath+'/page/3338web/memberInfo/memberIndex.jsp';
					}else{
						
					location.href = Helper.basePath+'/page/memberinfo/';
					}
				},
				onError: function(result, status){
					Helper.message(result.message);
				}
			});
			if(checkPwdBlur("oldPwd") && checkPwdBlur("newPwd") 
					&& notEqNewAndOldPwd() && checkReNewPwd()){
				updteLogPwd.addParameters("oldPwd", $("#IoldPwd").val());
				updteLogPwd.addParameters("newPwd", $("#InewPwd").val());
				updteLogPwd.send();
			}
		});
	};
	
	//支付密码 初始化
	commInit(2);
	payPwdInit();
	
});